import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { CategoryService } from './category.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { Result } from 'src/utils/Result';
import { ApiBody, ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';

@ApiTags('图书分类')
@Controller('category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @ApiOperation({description: '添加图书分类'})
  @ApiBody({ type: CreateCategoryDto })
  @Post('add')
  async create(@Body() createCategoryDto: CreateCategoryDto) {
    const result = await this.categoryService.create(createCategoryDto);
    return Result.success(result)
  }

  @ApiOperation({description: '获取图书分类下拉列表'})
  @Get('select_list')
  async findAll() {
    const result = await this.categoryService.findAll();
    return Result.success(result)
  }

  @ApiOperation({description: '分页获取图书分类列表'})
  @ApiBody({ schema: {
    default: {pageIndex: 0, pageSize: 10, category_name: ''}
  } })
  @Post('list')
  async findPageList(@Body() data: {pageIndex: number, pageSize: number, category_name: string}) {
    const result = await this.categoryService.findPageList(data);
    return Result.success(result)
  }

  @ApiOperation({description: '根据id获取图书分类详情'})
  @ApiQuery({name: 'id', required: true, description: '分类id'})
  @Get('query/:id')
  async findOne(@Param('id') id: string) {
    const result = await this.categoryService.findOne(+id);
    return Result.success(result)
  }

  @ApiOperation({description: '修改图书分类'})
  @ApiParam({name: 'id', required: true, description: '分类id'})
  @ApiBody({ type: UpdateCategoryDto })
  @Patch('update/:id')
  async update(@Param('id') id: string, @Body() updateCategoryDto: UpdateCategoryDto) {
    const result = await this.categoryService
    .update(+id, updateCategoryDto);
    return Result.success(result)
  }
  @ApiOperation({description: '根据id删除图书分类'})
  @ApiParam({name: 'id', required: true, description: '分类id'})
  @Delete('del/:id')
  async remove(@Param('id') id: string) {
    const result = await this.categoryService.remove(+id);
    return Result.success(result)
  }
}
